//
//  RTBaseNavigationController.swift
//  soapp
//
//  Created by zhuxietong on 2019/8/10.
//

import UIKit
import RTRootNavigationController


extension RTNavigationItemCustomizable where Self:UIViewController{

    
    public func rt_customBackItem(withTarget target: Any!, action: Selector!) -> UIBarButtonItem! {
        let img = UIImage(named:"nav_back_normal_default")
        return UIBarButtonItem(image: img, style: .plain, target: target, action: action)
    }
    
   

    public func customBackItem(withTarget target: Any!, action: Selector!) -> UIBarButtonItem! {
        let img = UIImage(named:"nav_back_normal_default")
        return UIBarButtonItem(image: img, style: .plain, target: target, action: action)
    }
}


func __RTSafeUnwrapViewController(controller:UIViewController) -> UIViewController{
    if let ctr = controller as? RTContainerController{
        return ctr.contentViewController
    }
    return controller
}


class RTNavigtion: NSObject {
    override init() {
        super.init()
        swizzle_rt_navigation()
    }
    public func swizzle_rt_navigation() {
        let slf = RTRootNavigationController.self
        guard slf == RTRootNavigationController.self else { return }
        let p = Selector(("_installsLeftBarButtonItemIfNeededForViewController:"))
        DispatchQueue.once(token: UUID().uuidString) {
            let needSwizzleSelectorArr = [                (p,#selector(RTRootNavigationController.et_installsLeftBarButtonItemIfNeeded)),
            ]
            
            for selector in needSwizzleSelectorArr {
                let originalMethod = class_getInstanceMethod(RTRootNavigationController.self, selector.0)
                let swizzledMethod = class_getInstanceMethod(RTRootNavigationController.self, selector.1)
                let didAddMethod: Bool = class_addMethod(slf, selector.0, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
                if didAddMethod {
                    class_replaceMethod(slf, selector.1, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
                } else {
                    method_exchangeImplementations(originalMethod!, swizzledMethod!)
                }
            }
        }
    }
}




extension RTRootNavigationController {
   
    
        
    @objc func rt_navigation_pop(){
        self.popViewController(animated: true)
    }
    
    
    @objc func et_installsLeftBarButtonItemIfNeeded(for vc: UIViewController!) {
        if let first = self.viewControllers.first{
            let isRootVc = vc == __RTSafeUnwrapViewController(controller: first)
            let hasSetLeftItem = vc.navigationItem.leftBarButtonItem != nil
            if (!isRootVc && !self.useSystemBackBarButtonItem && !hasSetLeftItem){
//                if vc.responds(to: Selector("rt_customBackItemWithTarget:action:")){
//
//                }
//                if vc.responds(to: Selector("customBackItemWithTarget:action:")){
//
//                }
                if vc.responds(to:#selector(rt_customBackItem(withTarget:action:))){
                    vc.rt_customBackItem(withTarget: self, action: #selector(rt_navigation_pop))
                }
                else{
                    vc.navigationItem.leftBarButtonItem = UIBarButtonItem(image: AppTheme.nav_back_img, style: .plain, target: self, action: #selector(rt_navigation_pop))
                }

            }
        }
            }
    
     public func _swizzle_rt_navigation() {
        let slf = RTRootNavigationController.self
        guard slf == RTRootNavigationController.self else { return }
        let p = Selector(("_installsLeftBarButtonItemIfNeededForViewController:"))
        DispatchQueue.once(token: UUID().uuidString) {
            let needSwizzleSelectorArr = [                (p,#selector(RTRootNavigationController.et_installsLeftBarButtonItemIfNeeded)),
            ]
            
            for selector in needSwizzleSelectorArr {
                let originalMethod = class_getInstanceMethod(RTRootNavigationController.self, selector.0)
                let swizzledMethod = class_getInstanceMethod(RTRootNavigationController.self, selector.1)
                let didAddMethod: Bool = class_addMethod(slf, selector.0, method_getImplementation(swizzledMethod!), method_getTypeEncoding(swizzledMethod!))
                if didAddMethod {
                    class_replaceMethod(slf, selector.1, method_getImplementation(originalMethod!), method_getTypeEncoding(originalMethod!))
                } else {
                    method_exchangeImplementations(originalMethod!, swizzledMethod!)
                }
            }
        }
    }
    
    
}

//extension RTNavigationItemCustomizable where Self:UIViewController
//{
//
//}

